import Vue from 'vue';
import VueRouter from 'vue-router';
import store from '@/store';
import Login from "view/users/Login";
import HomeLayouts from "lay/HomeLayouts";

Vue.use(VueRouter)

const routes = [
  {
    name: "login",
    path: "/login",
    component: Login
  },
  {
    name: "HomeLayouts",
    path: "/",
    component: HomeLayouts,
    children: [
      {
        path: "/index/index",
        name: "Home",
        component: () => import("view/home/index")
      },
      {
        path: "/image/index",
        name: "image",
        component: () => import("view/home/image")
      },
      {
        path: "/shop/goods/list",
        name: "list",
        component: () => import("view/goods/list")
      },
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
});

//路由守卫
router.beforeEach((to, from, next) => {
  const token = store.getters.token
  //如果有token 并且在登录页 那就跳转到首页
  if (token && to.name === "login") {
    return next({ name: "HomeLayouts" })
  }
  //如果没有 token 并且不在登录页 那就跳转到首页
  if (!token && to.name !== "login") {
    return next({ name: "login" })
  }
  next()
})

export default router
